#!/bin/bash
# Ubuntu 22.04 初始化脚本
# 适用于 VMware 虚拟机，包含常用工具安装和系统配置优化

set -e  # 遇到错误立即退出

main(){
  config_armhf_sources_list
  # 常用工具安装
  install_common_tools
}

# Ubuntu 20.04 armhf 官方源
config_armhf_sources_list(){
  cp /etc/apt/sources.list /etc/apt/sources.list.bak_$(date +%Y%m%d_%H%M%S)
  egrep -v "^#|^$" /etc/apt/sources.list
  cat <<EOF | sudo tee /etc/apt/sources.list
deb http://ports.ubuntu.com/ubuntu-ports/ focal main restricted
deb http://ports.ubuntu.com/ubuntu-ports/ focal-updates main restricted
deb http://ports.ubuntu.com/ubuntu-ports/ focal universe
deb http://ports.ubuntu.com/ubuntu-ports/ focal-updates universe
deb http://ports.ubuntu.com/ubuntu-ports/ focal multiverse
deb http://ports.ubuntu.com/ubuntu-ports/ focal-updates multiverse
deb http://ports.ubuntu.com/ubuntu-ports/ focal-backports main restricted universe multiverse
deb http://ports.ubuntu.com/ubuntu-ports/ focal-security main restricted
deb http://ports.ubuntu.com/ubuntu-ports/ focal-security universe
deb http://ports.ubuntu.com/ubuntu-ports/ focal-security multiverse
EOF

  apt-get update

}

install_common_tools(){
  # 安装常用工具
  # 提供 iwconfig/ifconfig 扫描 WiFi
  apt-get install -y wireless-tools
  # 新一代 WiFi 工具（iwlist, iw dev 等）
  apt-get install -y iw
   # 负责 WPA/WPA2/WPA3 认证
  apt-get install -y wpasupplicant
  # ifconfig/route 等
  apt-get install -y net-tools
  # ping 工具
  apt-get install -y iputils-ping
  # 获取 DHCP 地址
  apt-get install -y isc-dhcp-client
  # Ubuntu 20.04 默认的网络配置管理工具
  apt-get install -y netplan.io
  # git
  apt-get install -y git
  echo "=== 配置 Git 默认信息（如需修改请编辑此处）==="
  git config --global user.name "lushuan"
  git config --global user.email "lushuan2071@126.com"

  # 安装unzip
  apt install -y unzip

  # 安装python3-pip
  apt install -y python3-pip
  pip3 --version
  # 编译应用程序的3个工具
  apt install gcc
  apt install g++
  apt install make

  # 生产常用
  echo "=== 安装 Supervisor ==="
  apt-get install -y supervisor
  systemctl enable supervisor
  systemctl start supervisor

}

# 生产环境服务安装
install_prod_services() {
    echo "=== 安装 Supervisor ==="
    sudo apt-get install -y supervisor
    sudo systemctl enable supervisor
    sudo systemctl start supervisor

    echo "=== 安装 Docker > Ubuntu 18.04==="
    sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
    # 移除掉原来的 docker.list
    sudo rm -f /etc/apt/sources.list.d/docker.list
    sudo rm -f /usr/share/keyrings/docker-ce-archive-keyring.gpg

    # 使用清华镜像源
    echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-ce-archive-keyring.gpg] \
https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu jammy stable" \
| sudo tee /etc/apt/sources.list.d/docker.list

    # 获取清华提供的 GPG key
    curl -fsSL https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu/gpg \
      | sudo gpg --dearmor -o /usr/share/keyrings/docker-ce-archive-keyring.gpg

    sudo apt-get update -y
    sudo apt-get install -y docker-ce docker-ce-cli containerd.io
    #sudo apt-get install -y  containerd.io

    sudo systemctl enable docker
    sudo systemctl start docker
}

# 适配不同架构和不同Ubuntu 版本安装docker,Ubuntu 不同版本安装的docker 会识别glibc 版本
install_docker(){

  sources ./install_docker_smart.sh
  bash install_docker_smart.sh

}

# 允许 root 登录
enable_root_login() {
    echo "=== 允许 root 用户远程登录 ==="
    grep PermitRootLogin  /etc/ssh/sshd_config
    sudo sed -i 's/^#\?PermitRootLogin.*/PermitRootLogin yes/' /etc/ssh/sshd_config
    sudo systemctl restart sshd
}


main